package io.github.lee.jel.ui.item.detail

import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.graphics.Color
import android.os.Bundle
import android.widget.LinearLayout
import androidx.activity.viewModels
import androidx.appcompat.widget.Toolbar
import androidx.core.os.bundleOf
import androidx.databinding.ViewDataBinding
import dagger.hilt.android.AndroidEntryPoint
import io.github.lee.core.ui.BaseActivity
import io.github.lee.core.ui.databinding.ToolbarSimpleCenterTitleBinding
import io.github.lee.core.util.getBundleParcelable
import io.github.lee.core.util.toPx
import io.github.lee.core.vm.err.DataParseFailedException
import io.github.lee.jel.R
import io.github.lee.jel.databinding.ActivityItemDetailBinding
import io.github.lee.jel.databinding.LayoutGlobalLoadingBinding
import io.github.lee.jel.databinding.LayoutPeopleAvatarBinding
import io.github.lee.jel.domain.Item
import io.github.lee.jel.ui.item.play.MediaPlayActivity
import io.github.lee.jel.utils.screenSize
import io.github.lee.jel.utils.screenWidthDP

@AndroidEntryPoint
class ItemDetailActivity : BaseActivity<ActivityItemDetailBinding, ItemDetailVM>() {

    companion object {
        private const val KEY_ITEM = "ITEM"

        @JvmStatic
        fun start(context: Context, item: Item) {
            val starter = Intent(context, ItemDetailActivity::class.java)
            starter.putExtras(
                bundleOf(
                    KEY_ITEM to item
                )
            )
            context.startActivity(starter)
        }
    }

    //====
    override fun onCreateViewModel(): ItemDetailVM =
        viewModels<ItemDetailVM>().value

    override fun onCreateToolbar(): Toolbar =
        ToolbarSimpleCenterTitleBinding.inflate(layoutInflater)
            .also {
                it.bg = Color.TRANSPARENT
            }
            .toolbar

    override fun onCreateLoading(): ViewDataBinding =
        LayoutGlobalLoadingBinding.inflate(layoutInflater)

    override fun onCreateSuccess(): ActivityItemDetailBinding =
        ActivityItemDetailBinding.inflate(layoutInflater)


    //===

    private val a by viewModels<ItemDetailVM>()

    override fun onInitData(savedInstanceState: Bundle?) {
        super.onInitData(savedInstanceState)
        isHover = true

    }

    override fun onObserved() {
        super.onObserved()
        vm?.detailLiveData?.observe(this) {
            if (null == it) {
                showError(DataParseFailedException(mContext))
                return@observe
            }
            vb?.detail = it
            //处理演员显示
            vb?.llPeopleRoot?.removeAllViews()

            it.people?.forEachIndexed { index, people ->
                val itemView = LayoutPeopleAvatarBinding.inflate(layoutInflater)
                itemView.people = people
                val count = if (screenWidthDP(mContext) > 600) {
                    11
                } else {
                    6
                }
                val itemWidth = screenSize(mContext).width / count
                val params =
                    LinearLayout.LayoutParams(itemWidth, LinearLayout.LayoutParams.WRAP_CONTENT)
                when (index) {
                    0 -> params.marginStart = 16.toPx(mContext)
                    it.people.size - 1 -> {
                        params.marginStart = 8.toPx(mContext)
                        params.marginEnd = 16.toPx(mContext)
                    }

                    else -> params.marginStart = 8.toPx(mContext)

                }

                vb?.llPeopleRoot?.addView(itemView.root, params)
            }

            vb?.tvPlay?.setOnClickListener { _ ->
                val item = getBundleParcelable(KEY_ITEM, Item::class.java)
                if (null == item) {
                    showError(DataParseFailedException(mContext))
                    return@setOnClickListener
                }
                MediaPlayActivity.start(mContext, item)
            }
            showSuccess()
        }
    }

    override fun onSetViewData() {
        super.onSetViewData()
        statusBarColor(Color.TRANSPARENT, screenWidthDP(mContext) > 600, false)
        navigationBarColor(Color.TRANSPARENT, true)
        val tint = if (screenWidthDP(mContext) > 600) {
            getColor(R.color.main)
        } else {
            Color.WHITE
        }
        setNavigationIcon(R.drawable.outline_arrow_back_ios_new_24, tint) { finish() }
        toolbarFrameLayout.fitsSystemWindows = true

        showLoading()
        val item = getBundleParcelable(KEY_ITEM, Item::class.java)
        if (null == item) {
            showError(DataParseFailedException(mContext))
            return
        }
        vm?.itemDetail(item.id)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        statusBarColor(Color.TRANSPARENT, screenWidthDP(mContext) > 600, false)

        val tint = if (screenWidthDP(mContext) > 600) {
            getColor(R.color.main)
        } else {
            Color.WHITE
        }
        setNavigationIcon(R.drawable.outline_arrow_back_ios_new_24, tint) {
            finish()
        }
    }

}